{$R-,F-} (* Program "primes" computes the first 1000 prime numbers, and *)
(* writes them in a table with 20 numbers per line.  This takes 1347 *)
(* msec. on the CDC 6400 (1061 msec. without the range checking)     *)

(* Modified to 10 numbers per line. *)

PROGRAM PRIMES(OUTPUT);
CONST N = 1000;  N1 = 33;  (*N1 = SQUARE ROOT OF N*)
VAR I,K,X,INC,LIM,SQUARE,L,HI,LOW, q, iterations: INTEGER;
   PRIM: BOOLEAN;
   P,V: ARRAY[1..N1] OF INTEGER;
      
    SHITIME ,SLOWTIME, EHITIME, ELOWTIME: INTEGER;

BEGIN   
Writeln;
Writeln ('First 1000 primes.  No IO.  (R-)');
Writeln;
Write ('Iterations ?  (<cr> starts benchmark)');
Readln (iterations);

for q := 1 to iterations do begin
   (*
   WRITE(2:6, 3:6);
   *)
     L := 2;
      X := 1;  INC := 4;  LIM := 1;  SQUARE := 9;
      FOR I := 3 TO N DO
      BEGIN  (*FIND NEXT PRIME*)
         REPEAT X := X+INC;  INC := 6-INC;
            IF SQUARE <=X THEN
               BEGIN LIM := LIM +1;
                  V[LIM] := SQUARE;  SQUARE := SQR(P[LIM+1])
               END;
            K := 2;  PRIM := TRUE;
            WHILE PRIM AND (K<LIM) DO
            BEGIN K := K+1;
               IF V[K] < X THEN V[K] := V[K] + 2*P[K];
               PRIM := X <> V[K]
            END
         UNTIL PRIM;
         IF I <= N1 THEN P[I] := X;
         (*
         WRITE(X:6); 
         *)
         L := L+1;
         IF L = 10 THEN
            BEGIN (*
                   WRITELN;  
                   *)
                   L := 0;
            END
      END;
      (*
      WRITELN;  
      *)
   end {for q};

   WRITELN('DONE', CHR (7));

END.
